import pickle

import os

import codecs

import requests
import time

import sys

from datetime import datetime
from lxml import etree

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

import config_setting
import util_singlefile
from logger import Logger
from util_path import extract_filename_from_url

chrome_options = Options()
# chrome_options.add_argument("--headless=new")
driver = webdriver.Chrome(options=chrome_options)

sys.stdout = Logger(datetime.now().strftime("log/zhihuj_video_crawler_%Y%m%d_%H%M%S") + ".txt")
set_cookie_flag = False
# video_src = "https://www.zhihu.com/zvideo/1820515152988078080"

video_headers = {
    'Connection': 'Keep-Alive',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Language': 'zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36',
}
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
    "Connection": "keep-alive",
    "Accept": "text/html,application/json,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.8",
    'Cookie': "_xsrf=NDpW67ewntsdH1bsDqXHQOe9m4SsFPbM; _zap=a08a1bae-2b70-49f7-a9f1-765a0a4b1d57; d_c0=AWASfY9vlhmPTq7JBCezL-EsQ57mRTXW_f8=|1732372304; q_c1=65b7a2b4206e4618805136fade89a2b8|1732847702000|1732847702000; __snaker__id=05dKOfmcrmmyOVas; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1735048247,1735197005,1735392473,1735697312; HMACCOUNT=1B0B6845D0B894DA; tst=r; SESSIONID=jUvA3xuHcd1kNCrvtltXq69N56VlAO1Y3GnzwnjDf2C; JOID=UFgSBElb6STpylgrXF6LM4FeMr9AM7tSrfkiWRQ4jxKCgRtbLfxNVYvIXSxZOC1fjYLd6dfCjAR-Bq3y0ZbNKfU=; osd=V14dAEtc7yvtyF8tU1qJNIdRNr1HNbRWr_4kVhA6iBSNhRlcK_NJV4zOUihbPytQiYDa79jGjgN4Canw1pDCLfc=; __zse_ck=004_s52ACem5eOSu5Nv15kAnzFkixbITMxSWFBzTqNLFqrxAkE=/IvNKUHN/JzECUCva7qWT2MQJ8twWlpR1KPfH8wk1lUBkxOGWuzxHn4zzQ0ioEXMOl2/kNn99C4D6dTg3-TuBY5/w9fDOgIJq/m/TVJSud8l7LQHTv231Xa3nwi+cjMSRHIDDftgg0HO8AyT3TYk5j2XWkZKDrv4yPRPEvL+nSRCf9UCIibXqPztHSD9VBmkOH5VBWPLfqmqNIm12C; z_c0=2|1:0|10:1735697316|4:z_c0|80:MS4xTV94UkJBQUFBQUFtQUFBQVlBSlZUWG9LV0dqR0dDWUFESVN2VDJGQ0s0U1g3Zlhsd0F5X2tnPT0=|8da96c7f80afbfcc27fefe844b11b25dcc40e4976aab1d62f2cb3fdd549a0155; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1735697333; BEC=684e706569bf16169217bb2a788786f3"
}


def save_video(src, path):
    print(f"requesting video {src}")
    response = requests.request("GET", src, timeout=8, headers=video_headers)
    if response.status_code == 200:
        with open(path, 'wb') as f:
            f.write(response.content)
            # print(f"saved image to {img_path}")
        print(f"saved to {path}")
    else:
        print(f"error: status code {response.status_code}")


def process_html(src, set_cookie):
    print(f"requesting html {src}")
    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """
        Object.defineProperty(navigator, 'webdriver', {
          get: () => undefined
        })
      """
    })
    driver.get(src)
    ### 反注释这段代码，先打断点，在这里手动登入，将cookie保存下来
    cookies = driver.get_cookies()
    with open("cookies.pkl", "wb") as file:
        pickle.dump(cookies, file)
    ### 下一次只要读取cookie再刷新就是登入状态了
    if set_cookie:
        with open("cookies.pkl", "rb") as file:
            cookies = pickle.load(file)
            for cookie in cookies:
                driver.add_cookie(cookie)
        driver.refresh()

    content = driver.page_source
    tree = etree.HTML(content)
    video_array = tree.xpath("//video/@src")
    if len(video_array) > 0:
        return video_array[0]
        # video_link = video_array[0]
        # save_video(video_link)
    else:
        raise Exception("No video tags found")


def read_singlefile_html(local_html_path):
    with codecs.open(local_html_path, mode='r', encoding='utf-8') as f:
        content_head = f.read(2000)
    url_embeded = util_singlefile.get_url_from_content(content_head).strip()
    return url_embeded


base_path = f"E:/03.WebArchivesInboxVideo/___todo/{config_setting.batch_name}"
target_base_path = f"E:/03.WebArchivesInboxVideo/inbox/{config_setting.batch_name}"
if not os.path.isdir(target_base_path) and os.path.dirname(target_base_path) != "":
    os.makedirs(target_base_path)

for root, dirs, files in os.walk(base_path):
    for file in files:
        try:
            # 检查文件扩展名是否为.json
            if file.endswith('.html'):
                # 构造完整的文件路径
                file_path = os.path.join(root, file)
                video_html_src = read_singlefile_html(file_path)
                # video_html_src = "https://www.zhihu.com/question/57343498/answer/783744933"
                if not set_cookie_flag:
                    video_link = process_html(video_html_src, True)
                    set_cookie_flag = True
                else:
                    video_link = process_html(video_html_src, False)
                video_origin_path = extract_filename_from_url(video_link)
                _, file_extension = os.path.splitext(video_origin_path)
                file_name, _ = os.path.splitext(file)
                video_new_path = f"{target_base_path}/{file_name}{file_extension}"
                save_video(video_link, video_new_path)
        except Exception as ex:
            print(f"error: {file} {ex}")

print("done")
# local_html_path = "E:/03.WebArchivesInbox/inbox/20241102/(93 封私信 _ 80 条消息) 隐藏宇宙奥秘的π！ - 知乎__1f7b625959deddd37d6e55b48e4f2d46c8fd58bd.html"
# video_src = read_singlefile_html(local_html_path)
# process_html(video_src)